<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Victor
 * Date: 08.12.11
 * Time: 2:02
 * To change this template use File | Settings | File Templates.
 */

class Answer_Answer extends Core_BaseModel
{

   var $correctSign = '/q/';
    public function getAllAnswerToQuestion($idQuestion)
    {
        //echo "getAllAnswerToQuestion($idQuestion) <br/>";
        $pdo = $this->dataModule->getConnection();
        $sql = "SELECT `ans1`.`idanswers`,`ans1`.`answer`
FROM `dzundza_multiplechoicevariants` as `choiceans`,
`dzundza_answers` as `ans1`
where `choiceans`.`baseQuestion_idquestion` = :idQuestion
AND `ans1`.`idanswers` = `choiceans`.`answers_idanswers`;";

        $stn = $pdo->prepare($sql);
        $stn->setFetchMode(PDO::FETCH_ASSOC);
        $stn->bindParam(':idQuestion', $idQuestion, PDO::PARAM_INT);
        $stn->execute();
        $result = $stn->fetchAll();
         //var_dump($result);
        return $result;
    }

    public function calculateMark($answers, $correct)
    {

        $mark = 0;
        $totalQuestion = count($answers);
        foreach($answers as $idquestion => $value){
        if($answers[$idquestion] == $correct[$idquestion]){
         $mark++;
        }

        }
        return round(($mark/$totalQuestion)*100);


    }

    public function isCorrectAnswer($answer)
    {
        if(empty($answer) || strlen($answer) <= 3) throw new Exception('Answer is empty or length <= 3!');
        $sign = substr($answer,0,3);
        //echo "<pre>sign is $sign<br/></pre>";
        if($sign != $this->correctSign) return false; else return true;

    }

    public function removeSign($answer)
    {
        $answer = substr($answer,3);
        return $answer;
    }

    public function addAnswer($answer)
    {
        try{
            //echo "<pre>addAnswer($answer)</pre><br/>";
            $pdo= $this->dataModule->getConnection();
            $pdo->beginTransaction();
            $sql = "INSERT INTO `dzundza_answers`(`answer`)VALUES(:answer)";
            $stn = $pdo->prepare($sql);
            $stn->bindParam(':answer',$answer,PDO::PARAM_STR);
            $stn->execute();
            $result = $pdo->lastInsertId('idanswers');
            $pdo->commit();
            return $result;


        }catch(PDOException $e){
            $pdo->rollBack();
            echo "<pre>error - $e</pre><br/>";
        }
    }

    public function addAnswerToLesson($idBaseQuestion, $idAnswer, $isCorrect)
    {
        try{
            //echo "<pre>addAnswerToLesson($idBaseQuestion, $idAnswer, $isCorrect)<br/></pre>";
            $pdo= $this->dataModule->getConnection();
            $pdo->beginTransaction();
            $sql = "INSERT INTO `dzundza_multiplechoicevariants`(`baseQuestion_idquestion`,`answers_idanswers`,`isCorrect`)
                     VALUES (:idquestion,:idanswer,:isCorrect)";
            $stn = $pdo->prepare($sql);
            $stn->bindParam(':idquestion',$idBaseQuestion,PDO::PARAM_INT);
            $stn->bindParam(':idanswer',$idAnswer,PDO::PARAM_INT);
            $stn->bindParam(':isCorrect',$isCorrect,PDO::PARAM_BOOL);
            $result = $stn->execute();
            $pdo->commit();
            return $result;


        }catch(PDOException $e){
            $pdo->rollBack();
            echo "<pre>error - $e</pre>";

        }
    }

























}
